package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.finance.model.enums.FinanceEnum;
import com.yungu.swift.system.finance.model.param.AdminAccountFlowPageParam;
import com.yungu.swift.system.sys.dao.SysAccountPlatformMapper;
import com.yungu.swift.system.sys.model.dto.SysAccountPlatformDto;
import com.yungu.swift.system.sys.model.vo.SysAccountPlatformVo;
import com.yungu.swift.system.sys.model.vo.SysPlatformAccountFlowVo;
import com.yungu.swift.system.sys.service.SysAccountPlatformService;
import com.yungu.swift.utils.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

@Service
public class SysAccountPlatformServiceImpl extends BaseServiceImpl<SysAccountPlatformDto> implements SysAccountPlatformService {

    @Autowired
    private SysAccountPlatformMapper sysAccountPlatformMapper;

    @Override
    protected IMapper<SysAccountPlatformDto> getMapper() {
        return sysAccountPlatformMapper;
    }

    @Override
    public ResponseData<SysAccountPlatformDto> getByAppid(String appid) {
        List<SysAccountPlatformDto> platformDtoList = sysAccountPlatformMapper.list(MapUtils.build("appid", appid));
        if (CollectionUtils.isNotEmpty(platformDtoList)) {
            return ResponseData.buildSuccessResponse(platformDtoList.get(0));
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "", null);
    }

    @Override
    public ResponseData<SysAccountPlatformVo> statistics(AdminParam adminParam) {
        SysAccountPlatformVo sysAccountPlatformVo = new SysAccountPlatformVo();
        List<SysAccountPlatformDto> platformDtos = sysAccountPlatformMapper.list(MapUtils.convertObjToMap(adminParam));
        if (CollectionUtils.isNotEmpty(platformDtos)) {
            SysAccountPlatformDto platformDto = platformDtos.get(0);
            sysAccountPlatformVo.setExpenditureBalance(platformDto.getCashBalance());
            sysAccountPlatformVo.setCouponAccumulate(platformDto.getCouponAccumulate());
            sysAccountPlatformVo.setRefundAccumulate(platformDto.getRefundAccumulate());
            sysAccountPlatformVo.setRewardAccumulate(platformDto.getRewardAccumulate());
        }
        // 计算平台净收入
        sysAccountPlatformVo.setCashBalance(sysAccountPlatformMapper.netIncome(adminParam.getAppid()));
        return ResponseData.buildSuccessResponse(sysAccountPlatformVo);
    }

    @Override
    public ResponseData<PageVo<SysPlatformAccountFlowVo>> flowQueryPage(AdminAccountFlowPageParam flowPageParam) {
        PageBounds pageBounds = new PageBounds(flowPageParam.getPageNum(), flowPageParam.getPageSize());
        PageList<SysPlatformAccountFlowVo> pageList = sysAccountPlatformMapper.flowQueryPage(flowPageParam, pageBounds);
        PageVo<SysPlatformAccountFlowVo> pageVo = new PageVo(flowPageParam.getPageNum(), flowPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        fillDate(pageList);
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<List<SysPlatformAccountFlowVo>> flowExport(AdminAccountFlowPageParam flowPageParam) {
        List<SysPlatformAccountFlowVo> flowVoList = sysAccountPlatformMapper.flowQueryPage(flowPageParam);
        fillDate(flowVoList);
        return ResponseData.buildSuccessResponse(flowVoList);
    }

    /**
     * 补充数据含义
     *
     * @param list
     */
    private void fillDate(List<SysPlatformAccountFlowVo> list) {
        list.stream().forEach(accountFlowVo -> {
            accountFlowVo.setCourseDescription(FinanceEnum.getMsg(accountFlowVo.getCourse()));
            accountFlowVo.setTransactionType(Objects.equals(accountFlowVo.getFinanceInflow(), accountFlowVo.getUuid()) ? "收入" : "支出");
        });
    }
}